﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BussinessLogic;
using DomainModel;
using WebClasses.Pages;

public partial class Admin_AdminManage : NormalPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindingData();

            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                ChangeToUpdateStatus();
            }
        }        
    }

    private void BindingData()
    {
        rptManagerList.DataSource = this.LogicController.AdminManager.GetAllManagers();
        rptManagerList.DataBind();
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        this.LogicController.AdminManager.CreateNewManager(new DomainModel.Admin
        {
            AdminName = txtName.Text,
            AdminPwd = txtPwd.Text,
        });

        BindingData();
    }

    protected void rptManagerList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            this.LogicController.AdminManager.DeleteManagerByID(Convert.ToInt32(e.CommandArgument));
            BindingData();
        }
    }

    private void ChangeToUpdateStatus()
    {
        btnCreate.Visible = false;
        btnUpdate.Visible = true;
        int id = 0;
        int.TryParse(Request["ID"], out id);

        Admin admin = this.LogicController.AdminManager.GetManagerByID(id);

        txtName.Text = admin.AdminName;
        txtPwd.Text = admin.AdminPwd;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int id = 0;
        int.TryParse(Request["ID"], out id);

        Admin admin = this.LogicController.AdminManager.GetManagerByID(id);
        admin.AdminName = txtName.Text;
        admin.AdminPwd = txtPwd.Text;

        this.LogicController.AdminManager.UpdateAdmin(admin);
        BindingData();
        btnCreate.Visible = true;
        btnUpdate.Visible = false;
        txtName.Text = string.Empty;
        txtPwd.Text = string.Empty;
    }
}
